package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysOrder;
import com.ruoyi.project.system.domain.SysTask;
import com.ruoyi.project.system.domain.bo.ExpressBo;
import com.ruoyi.project.system.domain.bo.MyTaskBo;
import com.ruoyi.project.system.domain.vo.ExpressVo;
import com.ruoyi.project.system.domain.vo.MyTaskVo;
import com.ruoyi.project.system.service.ISysOrderService;
import com.ruoyi.project.system.service.ISysTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 配送任务Controller
 *
 * @author orange
 * @date 2022-04-08
 */
@RestController
@RequestMapping("/system/task")
public class SysTaskController extends BaseController {
  @Autowired private ISysTaskService sysTaskService;
  @Autowired private ISysOrderService orderService;

  /** 查询配送任务列表 */
  @PreAuthorize("@ss.hasPermi('system:task:list')")
  @GetMapping("/list")
  public TableDataInfo list(SysTask sysTask) {
    startPage();
    List<ExpressBo> list = sysTaskService.selectSysTaskList(sysTask);
    return getDataTable(list);
  }

  /** 导出配送任务列表 */
  @PreAuthorize("@ss.hasPermi('system:task:export')")
  @Log(title = "配送任务", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, SysTask sysTask) {
    List<ExpressBo> list = sysTaskService.selectSysTaskList(sysTask);
    ExcelUtil<ExpressBo> util = new ExcelUtil<ExpressBo>(ExpressBo.class);
    util.exportExcel(response, list, "配送任务数据");
  }

  /** 获取配送任务详细信息 */
  @PreAuthorize("@ss.hasPermi('system:task:query')")
  @GetMapping(value = "/{taskId}")
  public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
    return AjaxResult.success(sysTaskService.selectSysTaskByTaskId(taskId));
  }

  /** 新增配送任务 */
  @PreAuthorize("@ss.hasPermi('system:task:add')")
  @Log(title = "配送任务", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody SysTask sysTask) {
    return toAjax(sysTaskService.insertSysTask(sysTask));
  }

  /** 修改配送任务 */
  @PreAuthorize("@ss.hasPermi('system:task:edit')")
  @Log(title = "配送任务", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody SysTask sysTask) {
    return toAjax(sysTaskService.updateSysTask(sysTask));
  }

  /** 删除配送任务 */
  @PreAuthorize("@ss.hasPermi('system:task:remove')")
  @Log(title = "配送任务", businessType = BusinessType.DELETE)
  @DeleteMapping("/{taskIds}")
  public AjaxResult remove(@PathVariable Long[] taskIds) {
    return toAjax(sysTaskService.deleteSysTaskByTaskIds(taskIds));
  }

  @PreAuthorize("@ss.hasPermi('system:task:express')")
  @Log(title = "配送任务", businessType = BusinessType.EXPRESS)
  @PostMapping("/express")
  public AjaxResult express(@RequestBody ExpressVo expressVo) {

    SysTask sysTask = new SysTask();
    sysTask.setTaskId(expressVo.getTaskId());
    sysTask.setTaskStatus(1);
    sysTask.setExpressId(expressVo.getUserId());
    sysTask.setUpdateTime(new Date());

    sysTaskService.updateSysTask(sysTask);

    SysOrder order = orderService.selectSysOrderByOrderId(expressVo.getOrderId());
    order.setUpdateTime(new Date());
    order.setOrderStatus(7L);
    order.setExpressId(expressVo.getUserId());
    orderService.updateSysOrder(order);

    return AjaxResult.success();
  }

  @PreAuthorize("@ss.hasPermi('system:task:mylist')")
  @PostMapping("/getMyTask")
  public TableDataInfo getMyTask(@RequestBody MyTaskVo myTaskVo) {
    startPage();
    List<MyTaskBo> myTaskList = sysTaskService.getMyTaskList(myTaskVo.getUserId());
    return getDataTable(myTaskList);
  }

  @PreAuthorize("@ss.hasPermi('system:task:expresssuccess')")
  @Log(title = "配送任务", businessType = BusinessType.EXPRESS_SUCCESS)
  @GetMapping("/successArrive")
  public AjaxResult successArrive(@RequestParam("taskId") Long taskId) {

    SysTask sysTask = new SysTask();
    sysTask.setTaskId(taskId);
    sysTask.setTaskStatus(2);
    sysTask.setUpdateTime(new Date());

    ExpressBo expressBo = sysTaskService.selectSysTaskByTaskId(taskId);
    Long orderId = expressBo.getOrderId();
    SysOrder order = orderService.selectSysOrderByOrderId(orderId);
    order.setUpdateTime(new Date());
    order.setOrderStatus(9L);
    orderService.updateSysOrder(order);

    return toAjax(sysTaskService.updateSysTask(sysTask));
  }

  @PreAuthorize("@ss.hasPermi('system:task:expressfail')")
  @Log(title = "配送任务", businessType = BusinessType.EXPRESS_FAIL)
  @GetMapping("/failArrive")
  public AjaxResult failArrive(
      @RequestParam("taskId") Long taskId, @RequestParam("remark") String remark) {

    SysTask sysTask = new SysTask();
    sysTask.setTaskId(taskId);
    sysTask.setTaskStatus(3);
    sysTask.setUpdateTime(new Date());

    ExpressBo expressBo = sysTaskService.selectSysTaskByTaskId(taskId);
    Long orderId = expressBo.getOrderId();
    SysOrder order = orderService.selectSysOrderByOrderId(orderId);
    order.setBusinessRemark("骑手:" + remark);
    order.setUpdateTime(new Date());
    order.setOrderStatus(10L);
    orderService.updateSysOrder(order);

    return toAjax(sysTaskService.updateSysTask(sysTask));
  }
}
